import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Task {
    private int taskId;
    private int userId;
    private String title;
    private String description;
    private LocalDateTime createdAt;
    private LocalDateTime dueDate;
    private String priority;
    private List<String> tags;
    private boolean status;
    private LocalDateTime reminderTime;
    private String cachedString;
    private boolean isDirty = true;

    // 用于创建新任务
    public Task(int userId, String title, String description, String priority) {
        this.userId = userId;
        this.title = title;
        this.description = description;
        this.priority = priority;
        this.tags = new ArrayList<>();
        this.status = false;
        this.createdAt = LocalDateTime.now();
    }

    // Getters and Setters
    public int getTaskId() {
        return taskId;
    }

    public void setTaskId(int taskId) {
        this.taskId = taskId;
    }

    public int getUserId() {
        return userId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
        isDirty = true;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public LocalDateTime getDueDate() {
        return dueDate;
    }

    public void setDueDate(LocalDateTime dueDate) {
        this.dueDate = dueDate;
    }

    public String getPriority() {
        return priority;
    }

    public void setPriority(String priority) {
        this.priority = priority;
    }

    public List<String> getTags() {
        return tags;
    }

    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public LocalDateTime getReminderTime() {
        return reminderTime;
    }

    public void setReminderTime(LocalDateTime reminderTime) {
        this.reminderTime = reminderTime;
    }

    public boolean needsReminder() {
        if (dueDate == null || status) {
            return false;
        }

        LocalDateTime now = LocalDateTime.now();
        // 使用已设置的提醒时间
        return now.isAfter(reminderTime) && now.isBefore(dueDate) &&
                !status; // 只要任务未完成就可以提醒
    }

    public String getReminderMessage() {
        return String.format("您的待办事项 \"%s\" 将在15分钟后截止！\n\n详细信息：\n%s",
                title, this.toString());
    }

    @Override
    public String toString() {
        if (!isDirty && cachedString != null) {
            return cachedString;
        }

        StringBuilder sb = new StringBuilder();
        sb.append(title)
                .append(" (").append(priority).append(")")
                .append("\n描述: ").append(description);

        if (!tags.isEmpty()) {
            sb.append("\n标签: ").append(String.join(", ", tags));
        }

        if (dueDate != null) {
            sb.append("\n截止日期: ").append(
                    dueDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
        }

        if (reminderTime != null) {
            sb.append("\n提醒时间: ").append(
                    reminderTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
        }

        cachedString = sb.toString();
        isDirty = false;
        return cachedString;
    }
} 